import axios from "axios";
import {ElMessage} from "element-plus";
import {da} from "element-plus/es/locale/index";


const authItemName="access_token";



const defaultFailure =(message,code,url)=> {
    console.warn(`请求地址：${url},状态码：${code},错误信息：${message},`)
    ElMessage.warning(message)
}

const defaultError =(err)=> {
    console.error(err)
    ElMessage.warning("发生了一些错误")
}

function takeAccessToken(){
    const str = localStorage.getItem(authItemName)||sessionStorage.getItem(authItemName);
    if(!str)return null;
    const authObj = JSON.parse(str);
    if(authObj.expire<=new Date()){
        deleteAccessToken()
        ElMessage.warning("登录状态已过期，请重新登录")
        return null
    }
    return authObj.token
}

function storeAccessToken(token,remember,expire) {
    const authObj ={token:token,expire:expire};
    const str = JSON.stringify(authObj);
    if(remember)
        localStorage.setItem(authItemName,str);
    else
        sessionStorage.setItem(authItemName,str);
}

function deleteAccessToken() {
    localStorage.removeItem(authItemName);
    sessionStorage.removeItem(authItemName);
}

function accessHeader(){
    const token=takeAccessToken();
    return token? {'Authorization': `Bearer ${takeAccessToken()}`}:{}

}

function internalPost(url,data,header,success,failure=defaultFailure){
    axios.post(url,data,{headers:header}).then(({data})=>{
        if(data.code===200){
            success(data.data)
        }else {
            failure(data.message,data.code,url)
        }
    }).catch(err=>(err))
}

function internalGet(url,header,success,failure=defaultError){
    axios.get(url,{headers:header}).then(({data})=>{
        if(data.code===200){
            success(data.data)
        }else {
            failure(data.message,data.code,url)
        }
    }).catch(err=>(err))
}

function get(url,success,failure=defaultFailure){
    internalGet(url,accessHeader(),success,failure)
}

function post(url,data,success,failure=defaultFailure){
    internalPost(url,data,accessHeader(),success,failure)
}

function login(username,password,remember,success,failure=defaultFailure){
    // console.log("Attempting to log in with:", username, password); // 添加日志


    internalPost("/api/auth/login",{
        username: username,
        password: password,
    },{
        'Content-Type': 'application/x-www-form-urlencoded'
    },(data)=>{
        storeAccessToken(data.token,remember,data.expire)
        ElMessage.success(`登录成功，欢迎${data.username}`)
        success(data)
    },failure)
}

function logout(success,failure=defaultFailure){
    get('/api/auth/logout',()=>{
        deleteAccessToken()
        ElMessage.success("退出登录成功")
        success()
    },failure)
}

function unauthorized(){
    return !takeAccessToken()
}

// 新增支付专用的请求方法
function payPost(url, data, success, failure = defaultFailure) {
    axios.post(url, data, {
        headers: accessHeader(),
        responseType: 'text'  // 指定响应类型为文本
    }).then(response => {
        if (response.data) {
            success(response.data)
        } else {
            failure('支付创建失败')
        }
    }).catch(err => {
        defaultError(err)
        failure(err)
    })
}

export {login,logout,get,post,unauthorized,accessHeader,takeAccessToken,payPost}